//设置token
import { SET_TOKEN, SET_USER } from '../constants/'

export interface UserState {
  amount: number
  avatar: string
  nickname: string
  phone: string
  realName: string
  sex: string
  token: string
}
const INITIAL_STATE = {
  amount: 0,
  avatar: 'http://dummyimage.com/100x100',
  nickname: '不知道先森',
  phone: '未绑定',
  realName: '未绑定',
  sex: '未选择',
  token: '',
}

export default function user(state: UserState = INITIAL_STATE, action: any) {
  switch (action.type) {
    case SET_TOKEN: {
      const { token } = action.payload
      return { ...state, token }
    }
    case SET_USER: {
      const { avatar, nickname, phone, realName, sex } = action.payload
      const updatedAmount =
        action.payload.amount !== undefined
          ? action.payload.amount
          : state.amount
      return {
        ...state,
        //有就改变，没有就维持现状
        amount: updatedAmount,
        avatar,
        nickname,
        phone,
        realName,
        sex,
      }
    }
    default:
      return state
  }
}
